package com.solution.admin.product.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.PromotionTagEntity;
import com.solution.admin.product.service.PromotionTagService;
import com.solution.common.constants.PublicStatus.IS_DELETE;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品-活动标签信息web控制层
 * 
 * @author xu_cc
 * @date 2017-10-16 19:14:41
 */
@RequestMapping("/promotionTag")
@Controller
public class PromotionTagController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(PromotionTagController.class);

	@Autowired
	private PromotionTagService promotionTagService;

	/**
	 * 加载活动标签下拉列表
	 * 
	 * @param request
	 * @return List<PromotionTagEntity>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年1月13日 下午4:48:26
	 */
	@RequestMapping("/listPromotionTag")
	@ResponseBody
	public List<PromotionTagEntity> listPromotionTag(HttpServletRequest request) {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("status", STATUS.ENABLED.getId());
			List<PromotionTagEntity> PromotionTagList = promotionTagService.listPromotionTag(param);
			return PromotionTagList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 跳转商品-活动标签信息查询页面
	 * 
	 * @param PromotionTag
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	 * @date 2017-10-16 19:14:41
	 */
	@RequestMapping("/toListPromotionTagPage")
	public String toListPromotionTagPage(PromotionTagEntity promotionTag, HttpServletRequest request) {
		return "product/promotionTag_list";
	}

	/**
	 * 分页查询商品-活动标签信息列表
	 * 
	 * @param promotionTag
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author xu_cc
	 * @date 2017-10-16 19:14:41
	 */
	@RequestMapping("/listPromotionTagPage")
	@ResponseBody
	public JSONGrid listPromotionTagPage(PromotionTagEntity promotionTag, HttpServletRequest request) {
		// int pageNum =
		// Integer.parseInt(request.getParameter("page").toString());
		// int numPerPage =
		// Integer.parseInt(request.getParameter("rows").toString());
		int pageNum = 1;
		int numPerPage = 10000;
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("tagName", promotionTag.getTagName());
		try {
			PageBean pageBean = promotionTagService.listPromotionTagPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品-活动标签信息失败", logger, e);
		}
		return new JSONGrid();
	}

	/**
	 * 跳转新增页面
	 * 
	 * @return string 跳转页面路径
	 * @author xu_cc
	 * @date 2017-10-16 19:14:41
	 */
	@RequestMapping("/toAddPromotionTag")
	public String toAddPromotionTag(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/promotionTag_form";
	}

	/**
	 * 跳转编辑页面
	 * 
	 * @return string 跳转页面路径
	 * @author xu_cc
	 * @date 2017-10-16 19:14:41
	 */
	@RequestMapping("/toUpdatePromotionTag")
	public String toUpdatePromotionTag(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			PromotionTagEntity promotionTag = promotionTagService.getPromotionTagById(id);
			request.setAttribute("promotionTag", promotionTag);
		} catch (Exception e) {
			handleException("跳转编辑商品-活动标签信息页面错误", logger, e);
		}
		return "product/promotionTag_form";
	}

	/**
	 * 修改商品标签状态
	 * 
	 * @Title: modifyStatus
	 * @Description:(描述)
	 * @param request
	 * @param tagId
	 * @param status
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年10月16日 下午5:12:26
	 */
	@RequestMapping("/modifyStatus")
	@ResponseBody
	public BaseReturn modifyStatus(HttpServletRequest request, Long tagId, Integer status) {
		BaseReturn result = new BaseReturn();
		try {
			PromotionTagEntity promotionTag = new PromotionTagEntity();
			promotionTag.setId(tagId);
			promotionTag.setStatus(status);
			promotionTag.setModifier(getUserName());
			promotionTagService.updatePromotionTag(promotionTag);
			result.setCode(SysCode.SUCCESS);
			result.setMsg("活动标签状态修改成功！");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("活动标签状态修改异常！");
			return result;
		}
	}

	/**
	 * 删除标签
	 * 
	 * @Title: doDelTag
	 * @Description:(描述)
	 * @param request
	 * @param tagId
	 * @param status
	 * @return BaseReturn
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年10月16日 下午5:12:26
	 */
	@RequestMapping("/doDelTag")
	@ResponseBody
	public BaseReturn doDelTag(HttpServletRequest request, Long tagId) {
		BaseReturn result = new BaseReturn();
		try {
			PromotionTagEntity promotionTag = new PromotionTagEntity();
			promotionTag.setId(tagId);
			promotionTag.setIsDelete(IS_DELETE.DELETED.getId());
			promotionTag.setModifier(getUserName());
			promotionTagService.updatePromotionTag(promotionTag);
			result.setCode(SysCode.SUCCESS);
			result.setMsg("活动标签删除成功！");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("活动标签删除异常！");
			return result;
		}
	}

	/**
	 * 新增或保存商品-活动标签信息
	 * 
	 * @param request
	 *            请求对象
	 * @param promotionTagEntity
	 *            商品-活动标签信息实体
	 * @return
	 * @author xu_cc
	 * @date 2017-10-16 19:14:41
	 */
	@RequestMapping("/savePromotionTag")
	@ResponseBody
	public BaseReturn savePromotionTag(HttpServletRequest request, PromotionTagEntity promotionTagEntity) {
		BaseReturn result = new BaseReturn();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("tagName", promotionTagEntity.getTagName());
			List<PromotionTagEntity> list = promotionTagService.listPromotionTag(map);
			// 新增
			if (null == promotionTagEntity.getId() && null != list && list.size() > 0) {
				result.setCode(SysCode.FAILED);
				result.setMsg("存在相同名称的商品标签！");
				return result;
			}
			// 修改
			if (null != promotionTagEntity.getId() && null != list && list.size() > 0) {
				for (PromotionTagEntity tag : list) {
					// 验证是否是本身
					if (!promotionTagEntity.getId().equals(tag.getId())) {
						result.setCode(SysCode.FAILED);
						result.setMsg("存在相同名称的活动标签！");
						return result;
					}
				}
			}
			String id = request.getParameter("id");
			if (StringUtil.isNotEmpty(id)) {
				promotionTagEntity.setModifier(getUserName());
				promotionTagService.updatePromotionTag(promotionTagEntity);
			} else {
				promotionTagEntity.setStatus(STATUS.ENABLED.getId());
				promotionTagEntity.setCreater(getUserName());
				promotionTagEntity.setSortNo(1);
				promotionTagEntity.setModifier(getUserName());
				promotionTagService.savePromotionTag(promotionTagEntity);
			}
			result.setCode(SysCode.SUCCESS);
			result.setMsg("活动标签保存成功！");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(SysCode.ERROR);
			result.setMsg("活动标签保存异常！");
			return result;
		}
	}
}
